//
//  AyPhotosView.m
//  xWeibo
//
//  Created by alvis mac on 10/21/15.
//  Copyright © 2015 alvis. All rights reserved.
//

#import "AyPhotosView.h"
#import "AyPhotoView.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
#import "AyDefine.h"

@implementation AyPhotosView

- (instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        for (int i=0; i<9; i++) {
            AyPhotoView *photoView=[[AyPhotoView alloc] init];
            photoView.userInteractionEnabled=YES;
            photoView.tag=i;
              [photoView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTap:)]];
              [self addSubview:photoView];
        }
    }
    return self;
}


- (void)photoTap:(UITapGestureRecognizer *)recognizer
{
    NSInteger count=self.photos.count;
    NSMutableArray *myphotos=[NSMutableArray arrayWithCapacity:count];
    for (int i=0; i<count; i++) {
        MJPhoto *mjphoto=[[MJPhoto alloc]init];
        mjphoto.srcImageView=self.subviews[i];
        AyPhoto *ayphoto=self.photos[i];
        NSString *photoUrl = [ayphoto.thumbnail_pic stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        mjphoto.url=[NSURL URLWithString:photoUrl];
        [myphotos addObject:mjphoto];
    }
    
    MJPhotoBrowser *browser=[[MJPhotoBrowser alloc]init];
    browser.currentPhotoIndex=recognizer.view.tag;
    browser.photos=myphotos;
    [browser show];
    
}


- (void)setPhotos:(NSArray *)photos{
    _photos=photos;
    
    for (int i=0; i<self.subviews.count; i++) {
        AyPhotoView *photoView=self.subviews[i];
        if (i<photos.count) {
            photoView.hidden=NO;
            photoView.photo=photos[i];
            
            int maxColumns = (photos.count == 4) ? 2 : 3;
            int col = i % maxColumns;
            int row = i / maxColumns;
            CGFloat photoX = col * (AyPhotoW + AyPhotoMargin);
            CGFloat photoY = row * (AyPhotoH + AyPhotoMargin);
            photoView.frame = CGRectMake(photoX, photoY, AyPhotoW, AyPhotoH);
            
            if (photos.count == 1) {
                photoView.contentMode = UIViewContentModeScaleAspectFit;
                photoView.clipsToBounds = NO;
            } else {
                photoView.contentMode = UIViewContentModeScaleAspectFill;
                photoView.clipsToBounds = YES;
            }
            
        }else{
            photoView.hidden=YES;
        }
    }
}

+ (CGSize)photosViewSizeWithPhotosCount:(int)count
{
    // 一行最多有3列
    int maxColumns = (count == 4) ? 2 : 3;
    
    //  总行数
    int rows = (count + maxColumns - 1) / maxColumns;
    // 高度
    CGFloat photosH = rows * AyPhotoH + (rows - 1) * AyPhotoMargin;
    
    // 总列数
    int cols = (count >= maxColumns) ? maxColumns : count;
    // 宽度
    CGFloat photosW = cols * AyPhotoW + (cols - 1) * AyPhotoMargin;
    
    return CGSizeMake(photosW, photosH);
    /**
     一共60条数据 == count
     一页10条 == size
     总页数 == pages
     pages = (count + size - 1)/size;
     */
}

@end
